package com.lj.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lj.eduservice.client.OssClient;
import com.lj.eduservice.entity.Chapter;
import com.lj.eduservice.entity.Course;
import com.lj.eduservice.entity.CourseDescription;
import com.lj.eduservice.entity.Video;
import com.lj.eduservice.entity.vo.CourseInfoVo;
import com.lj.eduservice.mapper.CourseMapper;
import com.lj.eduservice.service.ChapterService;
import com.lj.eduservice.service.CourseDescriptionService;
import com.lj.eduservice.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.eduservice.service.VideoService;
import com.lj.utils.exception.APIException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author luojing
 * @since 2021-05-06
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseDescriptionService courseDescriptionService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OssClient ossClient;

    @Override
    @Transactional
    public void saveOrUpdateCourseInfo(CourseInfoVo courseInfoVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoVo,course);
        this.saveOrUpdate(course);

       courseInfoVo.setId(course.getId());

        CourseDescription courseDescription = new CourseDescription();
        BeanUtils.copyProperties(courseInfoVo,courseDescription);
        courseDescriptionService.saveOrUpdate(courseDescription);
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo = null;
        Course course = this.getOne(new QueryWrapper<Course>().eq("id", courseId));
        CourseDescription description = courseDescriptionService.getOne(new QueryWrapper<CourseDescription>().eq("id", courseId));
        if (course!=null){
            courseInfoVo = new CourseInfoVo();
            BeanUtils.copyProperties(course,courseInfoVo);
        }
        if (description!=null){
            BeanUtils.copyProperties(description,courseInfoVo);
        }
        return courseInfoVo;
    }

    /**
     * 删除课程需要先删除章节再删除描述，最后删除课程
     * @param courseId
     * @return
     */
    @Override
    @Transactional
    public Boolean removeCourseById(String courseId) {
        //查找课程对应的所有章节
        List<Chapter> chapters = chapterService.list(new QueryWrapper<Chapter>().eq("course_id", courseId));
        //删除章节
        for (Chapter chapter : chapters) {
            chapterService.deleteChapter(chapter.getId());
        }
        //删除描述
        courseDescriptionService.removeById(courseId);
        //删除封面
        Course byId = this.getById(courseId);
        ossClient.delete(byId.getCover().split("downloadFile")[1]);
        //删除课程
        this.removeById(courseId);

        return true;
    }
}
